{% extends "base/base.html" %}

{% comment %}

 Copyright 2013 - Tom Alessi

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and 
 limitations under the License.

{% endcomment %}

{% block content %}

<script>
  // Set edit style to inline instead of popup
  $.fn.editable.defaults.mode = 'inline';
</script>

<div class="row">
  {# This is a large-3 side nav #}
  {% include "admin/side_nav.html" %}

  <div class="large-9 columns">

    <div class="row">
      <div class="large-12 columns">
        <h1>Email Recipients</h1>
        <p>Use this page to manage email recipients.  Email addresses listed below will be available for sending alert emails to when creating incidents or maintenance events.</p>
        <hr>
      </div>
    </div>

    {# This is row consisting of 12 columns that will display all messages passed in the request #}
    {% include "admin/messages.html" %}

    <div class="row">
      <div class="large-12 columns">
        <span class="radius secondary label">Current Email Addresses</span><br><br>
        {% if emails %}
          {% for row in emails %}
          <div>
            <span style="width: 40px; float: left;">
              <a href="/admin/recipient_delete?id={{row.id}}" title="Delete email address"><span class="foundicon-gen-trash foundicon_container_iconlink"></span></a>
            </span>
            <span>
              <a href="#" title="Modify email address" id="email_{{row.id}}">{{row.email}}</a>
              <script>
              $(document).ready(function() {
                $('#email_{{row.id}}').editable({
                      url: '/admin/recipient_modify',
                      tpl: '<input type="text" maxlength="100">',
                      type: 'text',
                      pk: '{{row.id}}',
                      name: 'email',
                      inputclass: 'x_editable_text',
                      params: { csrfmiddlewaretoken: '{{csrf_token}}'},
                      error: function(response, newValue) {
                        return response.responseText;
                      }
                  });
              });
              </script>
            </span>
            <br><br>
          </div>
          <div class="spacer_micro"></div>
          {% endfor %}
        {% else %}
          <b>No email addresses defined</b>
        {% endif %}
      </div>
    </div>

    <div class="spacer_medium"></div>  

    <div class="row">
      <div class="large-12 columns">
        <span class="radius secondary label">Add Email Address</span><br>
        <div class="sublabel_container"><span class="sublabel">Enter a valid email address.</span></div>
      </div>
    </div>

    <div class="row">
      <div class="large-6 columns {% if form.email.errors %}error{% endif %}">
        <form method="POST" action="/admin/email_recipients">
         {% csrf_token %}
          <input name="email" type="text" placeholder="Enter a valid email address" maxlength="50" value="{% if form.email.data %}{{form.email.data}}{% endif %}">
          {% if form.email.errors %}
            <span class="err">{% for error in form.email.errors %}{{error}}<br>{% endfor %}<br></span>
          {% endif %}
          <input type="submit" class="small button secondary" value="add"/>
        </form> 
      </div>
    </div>

  </div>
</div>

{% endblock %}
